from sqlalchemy import Column, Integer, String, Sequence, ForeignKey, or_
from sqlalchemy.orm import Session

from app.db import db_query, db_update, Base


class Tag(Base):
    """
    标签表
    """
    # ID
    id = Column(Integer, Sequence('id'), primary_key=True, index=True)
    # 标签名
    name = Column(String)
    # 标签名(显示)
    name_display = Column(String)
    # 标签名(全称)
    full_name = Column(String)
    # 标签图标url
    icon_url = Column(String)
    group_id = Column(Integer)

    @staticmethod
    @db_query
    def get_by_name(db: Session, name: str):
        return db.query(Tag).filter(or_(Tag.name.__eq__(name), Tag.name_display.__eq__(name), Tag.full_name.__eq__(name))).first()

    @db_update
    def delete_by_name(self, db: Session, name: str):
        tag = self.get_by_name(db, name)
        if tag:
            tag.delete(db, tag.id)
        return True
